/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var topKFrequent = function (nums, k) {
    let map = new Map()
    nums.forEach((item) => {
        if (map[item]) {
            map[item] += 1
        } else {
            map[item] = 1
        }
    })
    let arr= []

    for (const val in map) {
        arr.push({
            label:val,
            value:map[val]
        })
    }
    arr.sort((a,b)=>{
       return b.value-a.value
    })
    console.log(arr);
    return arr.map(val=>val.label).slice(0,k)
};

console.log(topKFrequent([4,1,-1,2,-1,2,3], 2)); 